package com.example.bee_work_3.webc_ontent;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bee_work_3.net_request.MyJsonObject;
import com.example.bee_work_3.R;
import com.example.bee_work_3.net_request.WS;
import com.example.bee_work_3.account.LoginOrRegister;
import com.example.bee_work_3.collect.Collect;

public class WebActivity extends AppCompatActivity {
    private int id;
    private String url = null;
    private String title_text = "首页";

    private ProgressBar progressBar;
    private TextView title, menu;
    private WebView webView;
    private Button back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        init();
    }

    public void init() {
        menu = findViewById(R.id.web_menu);
        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        title = findViewById(R.id.web_title);
        progressBar = findViewById(R.id.web_progress);
        progressBar.setBackgroundColor(Color.RED);
        menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMenu();
            }
        });
        Intent intent = getIntent();
        url = intent.getStringExtra("URL");
        if (url == null) {
            url = "https://www.baidu.com";
        }
        title_text = intent.getStringExtra("title");
        if (title_text == null) {
            title_text = "首页";
        }
        id = intent.getIntExtra("id", 0);
        title.setText(title_text);
        webView = findViewById(R.id.webView);
        webView.loadUrl(url);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    public void showMenu() {
        PopupMenu popupMenu = new PopupMenu(this, menu);
        popupMenu.inflate(R.menu.menu_web);
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_collect:
                        if (id == 0)
                            break;
                        Collect.add(id, LoginOrRegister.COOKIESNAME, new WS.CallBack() {
                            @Override
                            public void onResult(final String Json) {
                                final MyJsonObject object = new MyJsonObject(Json);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (object.getInt("errorCode") == 0) {
                                            Log.e("Collect", "failed to collect;");
                                            Log.e("Collect detail", Json);

                                            Toast.makeText(WebActivity.this, "收藏失败", Toast.LENGTH_SHORT).show();
                                        } else {
                                            Log.e("Collect detail", Json);
                                            Log.e("Collect", "to collect;");
                                            Toast.makeText(WebActivity.this, "已收藏文章 id：" + id, Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }
                        });
                        break;
                    case R.id.action_refresh:
                        webView.reload();
                        break;
                }
                return false;
            }
        });
        popupMenu.show();
    }

    public void loading(String url) {
        webView.reload();
    }
}
